<?php
declare (strict_types = 1);

namespace app\model;

use app\constant\CommonConstant;
use app\constant\ModelConstant;
use think\Model;
use think\model\relation\HasOne;

/**
 * @property string $account
 * @property string $merchant_no
 * @property string $client_id
 * @property string $secrect
 * @property string $client_id_sandbox
 * @property string $secrect_sandbox
 * @property integer $shop_id
 * @property integer $status
 * @property integer $pay_method
 * @property integer $is_risk
 * @property HasOne $shop
 * @property string $merchant_no_sandbox
 * @property integer $mode
 * @property mixed $config
 * @property mixed $redirect_urls
 * @property string $pay_method_name
 * @property string $pay_method_name_en
 */
class ShopsPayment extends BaseModel
{

    protected $field = [
        'shop_id','account','merchant_no',
        'client_id','secrect','created_at',
        'updated_at','status', 'merchant_no_sandbox',
        'apply_status','mode','client_id_sandbox',
        'secrect_sandbox','pay_method','is_risk',
    ];

    protected $dateFormat = 'U';
    const PAY_METHOD_PAYPAL = 1;
    const PAY_METHOD_ASIABILL = 2;
    const PAY_METHOD_PAYONEER = 3;
    const PAY_METHOD_AIRWALLEX = 4;
    const PAY_METHOD_STRIPE = 5;
    const MODE_SANDBOX = 1;
    const MODE_LIVE = 2;
    const PAY_METHOD_DIANDIAN = 6;
    const PAY_METHOD_SPREEDLY = 7;

    public static $payMethodNames = [
        self::PAY_METHOD_PAYPAL =>'Paypal',
        self::PAY_METHOD_ASIABILL =>'Asiabill',
        self::PAY_METHOD_PAYONEER =>'Payoneer',
        self::PAY_METHOD_AIRWALLEX =>'Airwallex',
        self::PAY_METHOD_STRIPE =>'Stripe',
        self::PAY_METHOD_DIANDIAN => "DianDianPay",
        self::PAY_METHOD_SPREEDLY => "Spreedly",
    ];


    public function shop()
    {
        return $this->belongsTo(Shops::class,'shop_id');
    }

    public  function getConfigAttr()
    {
        return match ($this->mode){
            self::MODE_SANDBOX => [
                'merchant_no' => $this->merchant_no_sandbox,
                'app_key' => $this->client_id_sandbox,
                'app_secret' => $this->secrect_sandbox,
                'shop_id'=>$this->shop_id,
                'env'=>CommonConstant::MODE_SANDBOX_WORD ,
            ],
            self::MODE_LIVE => [
                'merchant_no' => $this->merchant_no,
                'app_key' => $this->client_id,
                'app_secret' => $this->secrect,
                'shop_id'=>$this->shop_id,
                'env'=>CommonConstant::MODE_LIVE_WORD
            ]
        };
    }

    public function scopeStatus($query,$status)
    {
        return $query->where('status',$status);
    }

    public function getRedirectUrlsAttr()
    {
        return 1;
    }

    public static function payment(int $shopId,$pay_method)
    {
        return self::query()
                   ->where('shop_id',$shopId)
                   ->where('pay_method',$pay_method)
                   ->where('status',ModelConstant::STATUS_ON)
                   ->findOrEmpty();
    }

    public function getPayMethodNameAttr()
    {
        return $this->pay_method > ShopsPayment::PAY_METHOD_PAYPAL ? CommonConstant::LOG_CREDIT_CARD : CommonConstant::LOG_PAYPAL;
    }

    //获取默认配置
    public static function getDefaultConfigByPayMethod($payMethod)
    {
        $defaultConfig = [
            self::PAY_METHOD_PAYPAL => [
                'merchant_no'=>'paypal',
                'client_id_sandbox'=>'AStgX20Bx4ZGVF7OovRksHtjOvCXxOz4F0KsE35TRu_v-JbMSO61cTfpAnFfQ9G5KhOA4CwiOgRzoYaW',
                'secrect_sandbox'=>'EPivtk3r7bBXQIJlkFEctTb_poZXZnNvkQ_Me87I185b9xHtdADMlPgy30sWicsD2kZXmNTlXc5MoWHM',
            ],
            self::PAY_METHOD_ASIABILL=>[
                'client_id_sandbox'=>'12246002',
                'secrect_sandbox'=>'12H4567r',
            ],
            self::PAY_METHOD_PAYONEER=>[
                'client_id_sandbox'=>'5124',
                'secrect_sandbox'=>'611v7e04qp2amjcl6a5vlebn10jssgt6b439s00r',
            ],
            self::PAY_METHOD_AIRWALLEX=>[
                'client_id_sandbox'=>'KTIm-D7vRh2FTtZmcWqfzA',
                'secrect_sandbox'=>'ccc58b08dbf177fa8e5f7b02e9459c7e07661995f534d0f3f576f69c69c488a922f07b5e071fd85d6412bf3ad082ecdd',
            ],
            self::PAY_METHOD_STRIPE=>[
                'client_id_sandbox'=>'pk_test_51P6rOI00foRxOwmPWZdWDyPt16K8ibj7KHpOy4UzsbEGaP7h0vvW7qHwNiZO8ov5tvt44iOV6uUfOPSvYZsXMfM800VDKb6B3J',
                'secrect_sandbox'=>'sk_test_51P6rOI00foRxOwmPw4aSgEBDIqOo1WU0ycdgWxWX3cbnPtqP7eviW01paUEKbtHkqvmWAn26g92FaIyAuCQHwUep000T7RNulo',
            ],
        ];
        return $defaultConfig[$payMethod];
    }

    public function getPayMethodNameEnAttr($pay_method)
    {
        return self::$payMethodNames[$this->pay_method] ?? 'paypal';
    }

}
